package com.example.mvp.view.login;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mvp.R;
import com.example.mvp.model.UserBean;
import com.example.mvp.precenter.login.LoginPrecenter;
import com.example.mvp.precenter.login.LoginPresenterImpl;

/**
 * Created by xzh on 2018/3/3.
 * 邮箱 ：347649749@qq.com
 * 版本 ： ver 1.0
 * 功能 ：
 */

public class LoginActivity extends AppCompatActivity implements LoginView {

    private LoginPrecenter precenter;
    private Dialog dialog;
    private OnButtonClickListener mClick;
    private TextView login,register;
    private EditText username,password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        mClick = new OnButtonClickListener();
        dialog = new Dialog(this);
        precenter = new LoginPresenterImpl(this);
        initView();
    }

    private void initView() {
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        username = findViewById(R.id.userName);
        password = findViewById(R.id.userPassword);

        login.setOnClickListener(mClick);
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this,str,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showProgress() {
       if (dialog != null) {
           dialog.show();
       }
    }

    @Override
    public void hideProgress() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public String getUserName() {
        return username.getText().toString();
    }

    @Override
    public String getPassWord() {
        return password.getText().toString();
    }

    @Override
    public void loginSuccess() {
    }

    @Override
    public void loginFailed() {

    }

    private class OnButtonClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.login:
                    //不是首次登录，登录数据理应保存在本地，
                    //首次安装登录，数据在服务器，需要请求服务器数据点击登录的时候
                    //在这里，假设请求网络或者获取本地返回的UserBean如下：
                    UserBean userBean = new UserBean("lisi","123456");
                    precenter.Login(userBean);
                    break;
                case R.id.register:
                    break;
                default:
                    break;
            }
        }
    }
}
